package com.sky.controller.admin;

import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * @Description
 * @Author shh
 * @Date 2025-10-06
 */
@Slf4j
@RestController
@Api(tags = "菜品相关接口")
@RequestMapping("/admin/dish")
public class DishController {

    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * @Description 添加菜品
     * @Author shh
     * @Date 2025-10-06
     */
    @ApiOperation("添加菜品")
    @PostMapping
    public Result addDish(@RequestBody DishDTO dishDTO) {
        log.info("添加菜品:{}", dishDTO);
        dishService.addDish(dishDTO);
        //缓存清除
        redisTemplate.delete("dish_" + dishDTO.getCategoryId());
        return Result.success();
    }

    /**
     * @Description 分页查询菜品
     * @Author shh
     * @Date 2025-10-06
     */
    @ApiOperation("分页查询菜品")
    @GetMapping("/page")
    public Result pageDish(DishPageQueryDTO dishPageQueryDTO) {
        log.info("分页查询菜品:{}", dishPageQueryDTO);
        PageResult pageResult = dishService.pageDish(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * @Description 批量删除菜品
     * @Author shh
     * @Date 2025-10-06
     */
    @ApiOperation("批量删除菜品")
    @DeleteMapping//用？ids=1,2,3,4,5的方式传递参数。
    public Result deleteDish(@RequestParam("ids") List<Long> ids) {//@RequestParam：表示该方法的参数ids将从请求参数中获取数据。
        // @PathVariable：表示该方法的参数dishId将从路径中获取数据。
        log.info("批量删除菜品:{}", ids);
        dishService.deleteDish(ids);
        //清除全部缓存
        Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);
        return Result.success();
    }
    /**
     * @Description 根据id查询菜品
     * @Author shh
     * @Date 2025-10-06
     */
    @ApiOperation("根据id查询菜品")
    @GetMapping("/{id}")
    public Result getDishById(@PathVariable("id") Long id) {
        log.info("根据id查询菜品:{}", id);
        DishVO dishvo = dishService.getDishById(id);
        return Result.success(dishvo);
    }
    /**
     * @Description 更新菜品
     * @Author shh
     * @Date 2025-10-06
     */
    @ApiOperation("更新菜品")
    @PutMapping
    public Result updateDish(@RequestBody DishDTO dishDTO) {
        log.info("更新菜品:{}", dishDTO);
        dishService.updateDish(dishDTO);
        //清除全部缓存
        Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);
        return Result.success();
    }

    /**
     * 根据分类id查询菜品
     *
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public Result<List<Dish>> list(Long categoryId) {
        List<Dish> list = dishService.list(categoryId);
        return Result.success(list);
    }

    /**
     * @Description 更新菜品状态
     * @Author shh
     * @Date 2025-10-06
     */
    @ApiOperation("更新菜品状态")
    @PostMapping("/status/{status}")
    public Result updateStatus(@PathVariable("status") Integer status,long id) {
        log.info("更新菜品状态:{}",status==1?"起售":"停售");
        dishService.updateStatus(status,id);
        //缓存清除
        //清除全部缓存
        Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);
        return Result.success();
    }
}
